/*
 * Web Crawler
 * Date: November 01, 2011
 * Group JEDi
 * Author(s): James Lovato, Efim Todorasco, Daniel Garrison
 */
package singlethread;
import java.util.ArrayList;
import java.util.List;

import multithread.DataMetrics;

import org.jsoup.nodes.Document;

/**
 * Class that processes a document looking for keys words within
 * noting how many times each word was found within the document.
 *
 * @author James Lovato, Efim Todorasco, Daniel Garrison
 * @version 10/28/11
 */
public class DataGatherer {
	/**
	 * The number of times each searched for word was found within the document.
	 */
	private int [] my_words_counted;
	
	/**
	 * The actual strings should not be needed.  The Reporter will need them however.
	 * The words the data gatherer is searching the document for.
	 */
	private String[] words_to_count;
	
	/**
	 * Total number of words for all paged parsed.
	 */
	private int my_total_words;
	
	/**
	 * Total number of links for all pages parsed.
	 */
	private int my_total_links;
	
	/**
	 * A list of DataMetrics objects.
	 */
	private List<DataMetrics> my_data_metrics;
	

	
	/**
	 * An object that gathers data from a given document by scanning the text, removing punctuation,
	 * and splitting the monolithic page string into a String[].
	 * 
	 * @param the_search_words A set of words this object will search for within a document.
	 */
	public DataGatherer(String[] the_search_words, int the_number_of_pages) {
		words_to_count = the_search_words;
		my_words_counted = new int[words_to_count.length];
		my_data_metrics = new ArrayList<DataMetrics>();
	}	
	
	public void submit(DataMetrics the_metrics) {
		int[] counts = the_metrics.hitCount();
		my_total_words += the_metrics.wordCount();
		my_total_links += the_metrics.linkCount();
		for (int i = 0; i < my_words_counted.length; i++) {
			my_words_counted[i] += counts[i];
		}
		my_data_metrics.add(the_metrics);
	}
	
	public int[] getWordCounts() {
		return my_words_counted;
	}	
	
	/**
	 * NOTE: THIS METHOD WILL BE DELETED AND MOVE TO CrawlerParser.countWords()
	 * Prepares the document for data gathering.
	 * Counts the number of "hit" words on the page and tallies them in the word_counter.
	 * @param the_doc The document to be processed.
	 */
	public void gatherData(final Document the_doc){
		my_words_counted = new int[words_to_count.length];
		String text;
		try {
			text = the_doc.body().text();
		} catch (NullPointerException e) {
			text = "";
		}
	    String text1 = text.replaceAll("[^\\p{L}]", " ");
	    text1 = text1.toLowerCase();
	    String s[] = text1.split("\\s+");
	    for (String st:s){
	    	for (int i = 0; i < words_to_count.length; i++) {
	            if (words_to_count[i].equals(st)) {
	            	my_words_counted[i]++;
	            }
	    	}
	    }
	}	
}
